package org.watchrecords.pages.crime;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.yui.calendar.DateField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.watchrecords.annotations.InstantiationPermission;
import org.watchrecords.constants.District;
import org.watchrecords.constants.Permission;
import org.watchrecords.dbo.Incident;
import org.watchrecords.dbo.Offender;
import org.watchrecords.elements.BasicForm;
import org.watchrecords.elements.list.ListEditor;
import org.watchrecords.elements.list.ListEditorItem;
import org.watchrecords.managers.IncidentManager;
import org.watchrecords.managers.OffenderManager;
import org.watchrecords.pages.BasicPage;
import org.watchrecords.util.EnumHelper;

import com.google.inject.Inject;

@InstantiationPermission(Permission.EDIT_INCIDENT)
public class EditIncidentPage extends BasicPage {
    @Inject private IncidentManager incidentManager;
    @Inject private OffenderManager offenderManager;
    
    public EditIncidentPage(PageParameters pageParameters) {
        super(pageParameters);
        Integer incidentId = getPageParameters() == null ? null : getPageParameters().getAsInteger("incidentId");
        Incident incident = incidentId == null ? new Incident() : incidentManager.getIncident(incidentId);
        
        add(new Label("header", incidentId == null ? "Add Incident": "Edit Incident"));
        add(new EditIncidentForm("form", incident));
        add(new FeedbackPanel("feedbackPanel"));
    }

    private class EditIncidentForm extends BasicForm<Incident> {
        /**
         * 
         */
        private static final long serialVersionUID = -7198275165833634656L;
        ListEditor<Offender> listEditor;
        
        public EditIncidentForm(String id, Incident inc) {
            super(id, inc);
            
            add(new DropDownChoice<District>("district", EnumHelper.getEnumList(District.class)));
            add(new DateField("date"));
            add(new TextField<String>("location").setRequired(false));
            add(new TextField<String>("watchman").setRequired(false));
            add(new TextField<String>("crime").setRequired(false));
            add(new TextField<String>("victim").setRequired(false));
            add(new TextField<String>("witnesses").setRequired(false));
            add(new TextField<String>("punishment").setRequired(false));
            add(new TextArea<String>("description").setRequired(false));
            
            listEditor = new ListEditor<Offender>("offenders", new PropertyModel<List<Offender>>(inc, "offenders")) {
                /**
                 * 
                 */
                private static final long serialVersionUID = 6504575016264234283L;

                @Override
                protected void onPopulateItem(ListEditorItem<Offender> item) {
                    DropDownChoice<Offender> select = new DropDownChoice<Offender>("offender", offenderManager.getOffenders());
                    select.setModel(new PropertyModel<Offender>(item, "modelObject"));
                    item.add(select);
                }
            };
            
            add(listEditor);

            add(new Button("cancel") {
                private static final long serialVersionUID = -7204043210850354788L;

                @Override
                public void onSubmit() {
                    setResponsePage(ViewIncidentsPage.class);
                }
            }.setDefaultFormProcessing(false));

            add(new Button("add") {
                private static final long serialVersionUID = -7204043210850354789L;

                @Override
                public void onSubmit() {
                    listEditor.addItem(new Offender());
                }
            }.setDefaultFormProcessing(false));
        }
        
        @Override
        protected void doSubmit() {
            getModelObject().getOffenders().clear();
            getModelObject().getOffenders().addAll(listEditor.getModelObject());
            incidentManager.saveIncident(getModelObject());
            setResponsePage(ViewIncidentsPage.class);
        }
    }
}
